﻿using System.Collections.Generic;
using System.Linq;
using PrototypeMetaMVVM.Web.Repository;
using PrototypeMetaMVVM.Web.Tools;

namespace PrototypeMetaMVVM.Web.Models
{
    public class ServerAccessPoint
    {
        static readonly ServerAccessPoint s_instance = new ServerAccessPoint();
        private readonly IRepository<Database> m_repository;

        public static ServerAccessPoint Instance
        {
            get { return s_instance; }
        }

        public ServerAccessPoint()
        {
            Tables = new List<Table>();
            Tables.Add(new Table("MyTable1"));
            Tables.Add(new Table("MyTable2"));
            Tables.Add(new Table("MyTable3"));

            m_repository = RepositoryBuilder.Create<Database>("Databases.xml");
            if (m_repository.Query().Count() == 0)
            {
                m_repository.Add(new Database("MyDB1", new List<Table> { Tables[0] }));
                m_repository.Add(new Database("MyDB2", new List<Table> { Tables[1], Tables[2] }));
            }

        }

        public List<Table> Tables { get; private set; }

        public IQueryable<Database> Databases
        {
            get
            {
                return m_repository.Query();
            }
        }

        public void Save()
        {
            m_repository.SubmitChanges();
        }
    }
}